package com.jckj.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.*;

/**
 * 通用工具类
 *
 * @author liuzhiyuan
 * @date 2018-01-02
 */
public class MyUtils {

    private static ThreadLocal<Boolean> JUMP = new ThreadLocal<Boolean>();

    /**
     * 读取properties文件中的内容并填充到map中
     *
     * @param fileName
     * @return
     */
    public static Map<String, String> convertPropertiesToMap(String fileName){
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        Properties properties = new Properties();
        if (classPathResource.exists()) {
            try {
                properties.load(classPathResource.getInputStream());
                Map<String, String> testMap = new HashMap<String, String>();
                Set<Map.Entry<Object, Object>> set = properties.entrySet();
                Iterator<Map.Entry<Object, Object>> it = set.iterator();
                String key = null, value = null;
                while (it.hasNext()) {
                    Map.Entry<Object, Object> entry = it.next();
                    key = String.valueOf(entry.getKey());
                    value = String.valueOf(entry.getValue());
                    key = key == null ? key : key.trim();
                    value = value == null ? value : value.trim();
                    LOGGER.info("Key:" + key);
                    LOGGER.info("Value: " + value);
                    testMap.put(key, value);
                }
                return testMap;
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    }
    private static Logger LOGGER = LoggerFactory.getLogger(MyUtils.class);

    /**
     * 设置是否本次请求跳转过滤方法
     * @param jumpFlag
     */
    public static void setJumpFlag(Boolean jumpFlag){
        JUMP.set(jumpFlag);
    }

    public static Boolean getJumpFlag(){
        return JUMP.get();
    }
}
